<?php

declare(strict_types=1);

namespace app\common\model;

use addons\Target\facade\Target;
use think\facade\Cache;
use think\facade\Event;

class User extends Base
{

    protected $name = 'user';

    protected $type = [
        'active_time' => 'timestamp',
    ];

    public function getPhotoAttr($value)
    {
        return $value ?: Cache::get('system')['default_photo'];
    }

    public function cate()
    {
        return 1;
    }
    public function getSexTextAttr($value, $data)
    {
        return $data['sex'] == 1 ? '男' : '女';
    }
    public function getCateTextAttr($value, $data)
    {
        return Cate::where('id', $data['cate'])->value('title')??"";
    }
    public static function _save($param, $logs)
    {
        parent::startTrans();
        try {
            if (!isset($param['sex'])) {
                return ['code' => -1, 'msg' => '请选择性别'];
            }
            if (!isset($param['cate'])) {
                return ['code' => -1, 'msg' => '请选择课程类型'];
            }
            if (isset($param['id']) && $param['id']) {
                $original = self::where($logs['pk'], $param[$logs['pk']])->find();
                if (empty($original)) {
                    return ['code' => -1, 'msg' => '记录不存在，请检查！'];
                }
                $count = parent::where(['mobile' => $param['mobile']])->where('id','<>',$param['id'])->count();
                if ($count) {
                    return ['code' => -1, 'msg' => '手机号已被注册'];
                }
                $logs['original'] = $original;
                $model = self::update($param);
                $logs['operation'] = 'save';
                if ($logs['writeLog']) {
                    $logs['model'] = $model;
                    Event::trigger('Logs', $logs);
                }
            } else {
                $code = generate(6, '', 1);
                $count = parent::where(['code' => $code])->count();
                if ($count) {
                    return ['code' => -1, 'msg' => '会员编号已被注册'];
                }
                $count = parent::where(['mobile' => $param['mobile']])->count();
                if ($count) {
                    return ['code' => -1, 'msg' => '手机号已被注册'];
                }
                $param['code'] = $code;
                $param['mobile'] = trim($param['mobile']);
                $param['rname'] = trim($param['rname']);
                $param['sex'] = trim($param['sex']);
                $param['age'] = trim($param['age']);
                $param['cate'] = trim($param['cate']);
                $param['birth'] = trim($param['birth']);
                $param['id_card'] = trim($param['id_card']);
                $param['content'] = trim($param['content']);
                $param['active_time'] = time();
                $param['is_active'] = 2;
                $param['password'] = !empty($param['password']) ? md5($param['password']) : md5('111111');
                $model=parent::create($param);
            }
            parent::commit();
            return ['code' => 1, 'msg' => '操作成功'];
        } catch (\Exception $e) {
            parent::rollback();
            return ['code' => -1, 'msg' => $e->getMessage()];
        }
    }

    public static function onAfterInsert($model)
    {
        Event::trigger('Statistics', ['key' => 'user', 'value' => 1]);
    }
}
